package com.example.myapplication2;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.myapplication2.fragment.ETCFragment;
import com.example.myapplication2.fragment.EnviFragment;
import com.example.myapplication2.fragment.IndexFragment;
import com.example.myapplication2.fragment.LightFragment;
import com.google.android.material.navigation.NavigationView;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private DrawerLayout drawerlayout;
    private NavigationView navigation;
    private FrameLayout framelayout;

    private TextView Texttemp;
    private Button button;
    private TextView Dayone;
    private TextView Daytwo;
    private String cunchu;
    private String qihou;
    private String wendu;
    private String fengxiang;
    private String fengli;

    private String riqichufaone;
    private String riqichufatwo;
    private String riqichufathere;
    private String riqichufafour;
    private String riqichufafive;
    private String riqichufasix;


    private String nian;
    private String yue;
    private String ri;


    OkHttpClient okHttpClient = new OkHttpClient();
    private ConstraintLayout Lout;
    private LinearLayout Layoutsix;
    private TextView TextDaysixteen;
    private ImageView imageseven;
    private TextView TextDayeighteen;
    private LinearLayout Layoutone;
    private TextView TextDayone;
    private ImageView imagetwo;
    private TextView TextDaythere;
    private LinearLayout Layouttwo;
    private TextView TextDayfour;
    private ImageView imagethere;
    private TextView TextDaysix;
    private LinearLayout LoutThere;
    private TextView TextDayseven;
    private ImageView imagefour;
    private TextView TextDaynine;
    private LinearLayout Loutfour;
    private TextView TextDaytwele;
    private ImageView imagefive;
    private TextView TextDaytwelve;
    private LinearLayout LoutFive;
    private TextView TextDaythirteen;
    private ImageView imagesix;
    private TextView TextDayfifteen;
    private TextView textView;
    private LinearLayout linearLayout;
    private TextView textView18;
    private ImageView imageone;
    private TextView Textwindone;
    private TextView Textwindtwo;
    private ImageView imagefengcheone;
    private ImageView imagefengchetwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerlayout,toolbar,R.string.app_name,R.string.app_name){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        toggle.syncState();
        drawerlayout.addDrawerListener(toggle);



        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout, IndexFragment.newInstance("",""),"")
                .commit();
        toolbar.setTitle("智能交通系统");

        navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setCheckable(true);
                switch (item.getItemId()) {
                    case R.id.Item_one:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, IndexFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("智能交通系统");
                        break;
                    case R.id.Item_two:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, ETCFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("ETC账户");
                        break;
                    case R.id.Item_there:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, LightFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("交通灯");
                        break;
                    case R.id.Item_four:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, EnviFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("环境指标");
                        break;
                    case R.id.Item_five:
                        MainActivity.this.finish();
                        break;
                }
                drawerlayout.closeDrawers();//关闭侧栏
                return true;
            }
        });


        /**----------------------------------------------------------------------------------**/

        /**-----------------------------------旋转动画-----------------------------------------------**/

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);

        imagefengcheone.startAnimation(animation);
        imagefengchetwo.startAnimation(animation);


        /**----------------------------------------------------------------------------------**/
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RequestBody requestBody = new FormBody.Builder()
                                .add("apiKey", "ijDE2g5a3e35965ca638f1115d95e6d51a0155f658307e9")
                                .add("area", "广州")
                                .build();
                        Request request = new Request.Builder()
                                .url("https://api.apishop.net/common/weather/get15DaysWeatherByArea")

                                .post(requestBody)
                                .build();
                        Call call = okHttpClient.newCall(request);
                        Log.i("TAG", "run:");
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                Log.i("TAG", "onFailure:" + e.getMessage());
                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {

                                        Gson gson = new Gson();
                                        Condition condition = gson.fromJson(result, Condition.class);



                                        String msg = condition.getResult().getDayList().get(0).getDay_air_temperature();
                                        Texttemp.setText(msg+"°C");

                                        cunchu = condition.getResult().getDayList().get(0).getDay_weather();
                                        Dayone.setText(cunchu);

                                        cunchu = condition.getResult().getDayList().get(0).getDay_wind_direction();
                                        Daytwo.setText(cunchu);


                                        /**-----------------------------------第一个日期-----------------------------------------------**/
                                        String riqichufaone = condition.getResult().getDayList().get(0).getDaytime();
                                        nian = riqichufaone.substring(0, 4);
                                        yue = riqichufaone.substring(4, 6);
                                        ri = riqichufaone.substring(6, 8);

                                        TextDayone.setText(nian + "年" + yue + "月" + ri + "日");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第二个日期-----------------------------------------------**/

                                        String riqichufatwo = condition.getResult().getDayList().get(1).getDaytime();
                                        nian = riqichufatwo.substring(0, 4);
                                        yue = riqichufatwo.substring(4, 6);
                                        ri = riqichufatwo.substring(6, 8);
                                        TextDayfour.setText(nian + "年" + yue + "月" + ri + "日");

                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第三个日期-----------------------------------------------**/
                                        String riqichufathere = condition.getResult().getDayList().get(2).getDaytime();
                                        nian = riqichufathere.substring(0, 4);
                                        yue = riqichufathere.substring(4, 6);
                                        ri = riqichufathere.substring(6, 8);
                                        TextDayseven.setText(nian + "年" + yue + "月" + ri + "日");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第四个日期-----------------------------------------------**/
                                        String riqichufafour = condition.getResult().getDayList().get(3).getDaytime();
                                        nian = riqichufafour.substring(0, 4);
                                        yue = riqichufafour.substring(4, 6);
                                        ri = riqichufafour.substring(6, 8);
                                        TextDaytwele.setText(nian + "年" + yue + "月" + ri + "日");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第五个日期-----------------------------------------------**/
                                        String riqichufafive = condition.getResult().getDayList().get(4).getDaytime();
                                        nian = riqichufafive.substring(0, 4);
                                        yue = riqichufafive.substring(4, 6);
                                        ri = riqichufafive.substring(6, 8);
                                        TextDaythirteen.setText(nian + "年" + yue + "月" + ri + "日");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第六个日期-----------------------------------------------**/
                                        String riqichufasix = condition.getResult().getDayList().get(5).getDaytime();
                                        nian = riqichufasix.substring(0, 4);
                                        yue = riqichufasix.substring(4, 6);
                                        ri = riqichufasix.substring(6, 8);
                                        TextDaysixteen.setText(nian + "年" + yue + "月" + ri + "日");
                                        /**----------------------------------------------------------------------------------**/

                                        /**-----------------------------------第一个天气-----------------------------------------------**/
                                        qihou = condition.getResult().getDayList().get(0).getDay_weather_code();
                                        if (qihou.equals("04")) {
                                            imagetwo.setImageResource(R.drawable.leizhenyu);
                                        }
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第二个天气-----------------------------------------------**/
                                        qihou = condition.getResult().getDayList().get(1).getDay_weather_code();
                                        if (qihou.equals("04")) {
                                            imagethere.setImageResource(R.drawable.leizhenyu);
                                        }
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第三个天气-----------------------------------------------**/
                                        qihou = condition.getResult().getDayList().get(2).getDay_weather_code();
                                        if (qihou.equals("01")) {
                                            imagefour.setImageResource(R.drawable.duoyun);
                                        }
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第四个天气-----------------------------------------------**/
                                        qihou = condition.getResult().getDayList().get(3).getDay_weather_code();
                                        if (qihou.equals("03")) {
                                            imagefive.setImageResource(R.drawable.zhenyu);
                                        }
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第五个天气-----------------------------------------------**/
                                        qihou = condition.getResult().getDayList().get(4).getDay_weather_code();
                                        if (qihou.equals("01")) {
                                            imagesix.setImageResource(R.drawable.duoyun);
                                        }
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第六个天气-----------------------------------------------**/
                                        qihou = condition.getResult().getDayList().get(5).getDay_weather_code();
                                        if (qihou.equals("01")) {
                                            imageseven.setImageResource(R.drawable.duoyun);
                                        }
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第一个温度-----------------------------------------------**/
                                        wendu = condition.getResult().getDayList().get(0).getDay_air_temperature();
                                        TextDaythere.setText(wendu+"°C");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第二个温度-----------------------------------------------**/
                                        wendu = condition.getResult().getDayList().get(1).getDay_air_temperature();
                                        TextDaysix.setText(wendu+"°C");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第三个温度-----------------------------------------------**/
                                        wendu = condition.getResult().getDayList().get(2).getDay_air_temperature();
                                        TextDaynine.setText(wendu+"°C");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第四个温度-----------------------------------------------**/
                                        wendu = condition.getResult().getDayList().get(3).getDay_air_temperature();
                                        TextDaytwelve.setText(wendu+"°C");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第五个温度-----------------------------------------------**/
                                        wendu = condition.getResult().getDayList().get(4).getDay_air_temperature();
                                        TextDayfifteen.setText(wendu+"°C");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第六个温度-----------------------------------------------**/
                                        wendu = condition.getResult().getDayList().get(5).getDay_air_temperature();
                                        TextDayeighteen.setText(wendu+"°C");
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第一个风向-----------------------------------------------**/
                                        fengxiang = condition.getResult().getDayList().get(0).getDay_wind_direction();
                                        Textwindone.setText("风向:"+fengxiang);
                                        /**----------------------------------------------------------------------------------**/
                                        /**-----------------------------------第一个风力-----------------------------------------------**/
                                        fengli = condition.getResult().getDayList().get(0).getDay_wind_power();
                                        Textwindtwo.setText("风力:"+fengli);
                                        /**----------------------------------------------------------------------------------**/





                                        Log.i("TAG", "run" + result);
                                    }
                                });

                            }
                        });

                    }


                }).start();
            }
        });
    }

    private void initView() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        navigation = (NavigationView) findViewById(R.id.navigation);
        framelayout = (FrameLayout) findViewById(R.id.framelayout);


        Texttemp = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
        Dayone = findViewById(R.id.Dayone);
        Daytwo = findViewById(R.id.Daytwo);

        Lout = (ConstraintLayout) findViewById(R.id.Lout);
        Layoutsix = (LinearLayout) findViewById(R.id.Layoutsix);
        TextDaysixteen = (TextView) findViewById(R.id.TextDaysixteen);
        imageseven = (ImageView) findViewById(R.id.imageseven);
        TextDayeighteen = (TextView) findViewById(R.id.TextDayeighteen);
        Layoutone = (LinearLayout) findViewById(R.id.Layoutone);
        TextDayone = (TextView) findViewById(R.id.TextDayone);
        imagetwo = (ImageView) findViewById(R.id.imagetwo);
        TextDaythere = (TextView) findViewById(R.id.TextDaythere);
        Layouttwo = (LinearLayout) findViewById(R.id.Layouttwo);
        TextDayfour = (TextView) findViewById(R.id.TextDayfour);
        imagethere = (ImageView) findViewById(R.id.imagethere);
        TextDaysix = (TextView) findViewById(R.id.TextDaysix);
        LoutThere = (LinearLayout) findViewById(R.id.LoutThere);
        TextDayseven = (TextView) findViewById(R.id.TextDayseven);
        imagefour = (ImageView) findViewById(R.id.imagefour);
        TextDaynine = (TextView) findViewById(R.id.TextDaynine);
        Loutfour = (LinearLayout) findViewById(R.id.Loutfour);
        TextDaytwele = (TextView) findViewById(R.id.TextDaytwele);
        imagefive = (ImageView) findViewById(R.id.imagefive);
        TextDaytwelve = (TextView) findViewById(R.id.TextDaytwelve);
        LoutFive = (LinearLayout) findViewById(R.id.LoutFive);
        TextDaythirteen = (TextView) findViewById(R.id.TextDaythirteen);
        imagesix = (ImageView) findViewById(R.id.imagesix);
        TextDayfifteen = (TextView) findViewById(R.id.TextDayfifteen);
        textView = (TextView) findViewById(R.id.textView);
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        textView18 = (TextView) findViewById(R.id.textView18);
        imagefengchetwo = (ImageView) findViewById(R.id.imagefengchetwo);
        imagefengcheone = (ImageView) findViewById(R.id.imagefengcheone);
        Textwindone = (TextView) findViewById(R.id.Textwindone);
        Textwindtwo = (TextView) findViewById(R.id.Textwindtwo);
    }
}